package array;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;

public class leetCode150_evalRPN {

    public static void main(String[] args) {
        leetCode150_evalRPN a = new leetCode150_evalRPN();
        String[] nums = {"4","13","5","/","+"};
        a.evalRPN(nums);
    }
    public int evalRPN(String[] tokens) {
        if(tokens == null || tokens.length == 0) return 0;
        Deque<Integer> deque = new ArrayDeque<>();
        for (String token : tokens) {
            if(isNumber(token)){
                deque.push(Integer.parseInt(token));
            }else {
                Integer a = deque.pop();
                Integer b = deque.pop();
                if (token.equals("+")) {
                    deque.push(a + b);
                    continue;
                }
                if (token.equals("-")) {
                    deque.push(a - b);
                    continue;
                }
                if (token.equals("*")) {
                    deque.push(a * b);
                    continue;
                }
                if (token.equals("/")) {
                    deque.push(a / b);
                }
            }
        }
        return deque.pop();
    }

    private boolean isNumber(String s){
        return !s.equals("+") && !s.equals("-") && !s.equals("*") && !s.equals("/");
    }

}
